package com.yksj.healthtalk.ui.friend;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;

import com.yksj.healthtalk.comm.RootFragment;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;

/**
 * 医生馆
 * 
 * @author Administrator
 * 
 */
public class RegisterMainFragment extends RootFragment implements
		OnClickListener {

	private CustomerInfoEntity mCustomerInfoEntity;// 搜索所需的数据
	private Bundle bundle;
	private FrameLayout fyt;
	private View v;
	private ArrayList<String> fragmentName;
	private Fragment fragment;
	private CheckBox mRegisterCheckBtn;
	private CheckBox mUnRegisterCheckBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.register_main_layout, null);
		initWidget(v);
		initData(v);
		addFragment(RegisterFragment.class, mCustomerInfoEntity);
		return v;
	}

	private void initData(View v) {
		mCustomerInfoEntity = new CustomerInfoEntity();
		titleTextV.setText(R.string.doctor_pavilion);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setBackgroundResource(R.drawable.icon_search);
		fragmentName = new ArrayList<String>();
		// rp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(RadioGroup group, int checkedId) {
		// if (mRegisterRb.isChecked()) {
		// addFragment(RegisterFragment.class, mCustomerInfoEntity);
		// }else if(mUnRegisterRb.isChecked()){
		// addFragment(unRegisterFragment.class, mCustomerInfoEntity);
		// }
		// }
		// });

	}

	private void initWidget(View v) {
		initTitleView(v);
		mRegisterCheckBtn = (CheckBox) v.findViewById(R.id.register_rb);
		mUnRegisterCheckBtn = (CheckBox) v.findViewById(R.id.unregister_rb);
		fyt = (FrameLayout) v.findViewById(R.id.fyt);
		initClickListener(v);
	}

	private void initClickListener(View v) {
		// TODO Auto-generated method stub
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		mRegisterCheckBtn.setOnClickListener(this);
		mUnRegisterCheckBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed(v);
			break;
		case R.id.title_right2:
			Intent intent2 = new Intent();
			intent2.putExtra("mCustomerInfoEntity", mCustomerInfoEntity);
			intent2.setClass(getActivity(), DoctorSearchMainActivity.class);
			startActivityForResult(intent2, 0);
			break;
		case R.id.register_rb:
			addFragment(RegisterFragment.class, mCustomerInfoEntity);
			break;
		case R.id.unregister_rb:
			addFragment(unRegisterFragment.class, mCustomerInfoEntity);
			break;
		default:
			break;
		}
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		destroyFragment(fragmentName);
		super.onDestroyView();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && resultCode == 0) {
			if (data != null && data.hasExtra("mCustomerInfoEntity")) {
				mCustomerInfoEntity = (CustomerInfoEntity) data.getExtras()
						.get("mCustomerInfoEntity");
				if (fragmentName.get(0).toString()
						.equals(RegisterFragment.class.getName())) {
					addFragment(RegisterFragment.class, mCustomerInfoEntity);
				} else {
					addFragment(unRegisterFragment.class, mCustomerInfoEntity);
				}

			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void addFragment(Class<?> clss,
			CustomerInfoEntity mCustomerInfoEntity) {
		destroyFragment(fragmentName);
		fragmentName.clear();
		if (clss.toString().equals(RegisterFragment.class.toString())) {
			fragment = RegisterFragment.getInstance(mCustomerInfoEntity);
			// mRegisterRb.setChecked(true);
			checkId(mRegisterCheckBtn);
		} else {
			fragment = unRegisterFragment.getInstance(mCustomerInfoEntity);
			checkId(mUnRegisterCheckBtn);
		}
		fragmentName.add(clss.getName());
		FragmentTransaction transaction = getFragmentManager()
				.beginTransaction();
		transaction.add(R.id.fyt, fragment);
		transaction.commit();
	}

	public void destroyFragment(ArrayList<String> fragmentName) {
		if (fragmentName.size() != 0) {
			FragmentTransaction transaction = getFragmentManager()
					.beginTransaction();
			transaction.remove(fragment);
			transaction.commitAllowingStateLoss();
		}
	}
	
	/**
	 * 点击按钮
	 * @param box
	 */
	void checkId(CheckBox box) {
		mRegisterCheckBtn.setChecked(false);
		mUnRegisterCheckBtn.setChecked(false);
		box.setChecked(true);
		mRegisterCheckBtn.setTextColor(getResources().getColor(R.color.tab_txt_gray));
		mUnRegisterCheckBtn.setTextColor(getResources().getColor(
				R.color.tab_txt_gray));
		box.setTextColor(getResources().getColor(R.color.tab_txt_orange));
	}
}
